package com.volunteer.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.volunteer.entity.Evaluation;
import com.volunteer.mapper.EvaluationMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 评价服务
 */
@Service
@RequiredArgsConstructor
public class EvaluationService {

    private final EvaluationMapper evaluationMapper;

    /**
     * 获取活动的评价列表
     */
    public List<Evaluation> getActivityEvaluations(Long activityId) {
        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Evaluation::getActivityId, activityId)
                .orderByDesc(Evaluation::getCreateTime);
        return evaluationMapper.selectList(queryWrapper);
    }

    /**
     * 创建评价
     */
    public Evaluation createEvaluation(Evaluation evaluation) {
        // 检查是否已评价
        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Evaluation::getActivityId, evaluation.getActivityId())
                .eq(Evaluation::getUserId, evaluation.getUserId());
        if (evaluationMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("您已评价过该活动");
        }

        evaluationMapper.insert(evaluation);
        return evaluation;
    }

    /**
     * 删除评价
     */
    public void deleteEvaluation(Long id) {
        evaluationMapper.deleteById(id);
    }
}

